#include "TCanvas.h"
#include "TH2F.h"
#include "TTree.h"
#include "TChain.h"
#include "TFile.h"
#include "lhcbStyle.h"
#include "TStyle.h"
#include "TColor.h"
#include "TF2.h"
#include "TExec.h"
#include "TCanvas.h"
#include "TSystem.h"
#include "../../MarvelSim/source/AnalysisManager/include/SimEvent.h"
const int NX = 72;
const int NY = 72;
const int el = 5;
const TString tail = "MyDNA";
 
void Pal1()
{
   static Int_t  colors[50];
   static Bool_t initialized = kFALSE;
 
   Double_t Red[3]    = { 0.00, 0.00, 1.00};
   Double_t Green[3]  = { 0.00, 1.00, 0.00};
   Double_t Blue[3]   = { 1.00, 0.00, 0.00};
   Double_t Length[3] = { 0.00, 0.50, 1.00 };
 
   if(!initialized){
      Int_t FI = TColor::CreateGradientColorTable(3,Length,Red,Green,Blue,200);
      for (int i=0; i<200; i++) colors[i] = FI+i;
      initialized = kTRUE;
      return;
   }
   gStyle->SetPalette(200,colors);
}


void DrawPlot(){
    gSystem->Load("../../MarvelSim/build/libSimEventDict.so");
    lhcbStyle();
    TString NRList[6] = {"H_Z1A1", "He_Z2A4", "C_Z6A12", "O_Z8A16", "D_Z1A2", "CheckTails"};
    TString NRName = NRList[el];
    if(access(NRName.Data(), 00) == -1) gSystem->Exec(Form("mkdir %s", NRName.Data()));
    TCanvas* can = new TCanvas("", "", 289, 289, 289, 289);//288
    can->SetMargin(0,0,0,0);
    //TFile* f = new TFile(Form("~/WorkSpace/Marvel/NRBKG/%s_wide/%s_1_feature.root", NRName.Data(), NRName.Data()));
    //TFile* f = new TFile(Form("/home/mart/WorkSpace/Marvel/NRBKG/CheckTails/H_%s_feature.root", tail.Data()));
    //TTree* tree = (TTree*)f->Get("t_data");
    TChain* tree = new TChain("t_data");
    tree->Add("/home/mart/WorkSpace/Marvel/NRBKG/CheckTails/H_10keV_feature.root");
    tree->Add("/home/mart/WorkSpace/Marvel/NRBKG/CheckTails/H_10keV_2_feature.root");
    tree->Add("/home/mart/WorkSpace/Marvel/NRBKG/CheckTails/H_10keV_3_feature.root");
    tree->Add("/home/mart/WorkSpace/Marvel/NRBKG/CheckTails/H_10keV_4_feature.root");
    tree->Add("/home/mart/WorkSpace/Marvel/NRBKG/CheckTails/H_10keV_5_feature.root");
    Short_t data[NX][NY];
    int sumADC;
    int lenght;
    tree->SetBranchAddress("sumADC",&sumADC);
    tree->SetBranchAddress("data", &data);
    tree->SetBranchAddress("length",&lenght);
    int Numbers = tree->GetEntries();
    
    for(int i = 0; i < Numbers; i++){
        tree->GetEntry(i);
        if(sumADC>16000 && lenght>3){
            TH2F* hPic = new TH2F("", "", 72, 1, 72, 72, 1, 72);
            for(int ix = 0; ix < 72; ix++){
                for(int iy = 0; iy < 72; iy++){
                    hPic->SetBinContent(ix+1, iy+1, data[ix][iy]);
                }
            }

            hPic->SetMinimum(10); // 设置colorbar最小值为0
            hPic->SetMaximum(2600); // 设置colorbar最大值为2600

            hPic->GetXaxis()->SetLabelOffset(999);
            hPic->GetXaxis()->SetLabelSize(0);
            hPic->GetYaxis()->SetLabelOffset(999);
            hPic->GetYaxis()->SetLabelSize(0);
            hPic->GetXaxis()->Delete();
            hPic->GetYaxis()->Delete();
            hPic->SetStats(kFALSE);

            can->SetLogz(); // 将colorbar设置为对数坐标

            can->SetFrameLineWidth(0);
            can->SetRealAspectRatio();
            can->cd();
            hPic->Draw("cola"); // 绘制2D图像，colz表示使用颜色填充

            TString filename = Form("./%s/%06d.png", NRName.Data(), i); // 根据循环索引创建文件名
            can->Update();
            can->SaveAs(filename); // 保存为png格式的图片

            delete hPic; // 释放内存
        }
        // delete can; // 释放内存
    }
}
